package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("travelCommend")
public class TravelCommendController {
    //关联service
    @Autowired
    private ITravelCommendService travelCommendService;

    //游记推荐页面
    @RequestMapping("list")
    public String list(@ModelAttribute("qo")TravelCommendQueryObject qo, Model model){
        qo.setOrderby("t.schedule desc");
        PageInfo<TravelCommend> pageInfo = travelCommendService.queryForList(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travelCommend/list";
   }

   //已发布页面
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(TravelCommend travelCommend, MultipartFile file){
        //判断是否上传图片
        if(file!=null&&file.getSize()>0){
            //获取上传的图片
            String url = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            //将新图片设置进游记推荐对象中
            travelCommend.setCoverUrl(url);
        }
        travelCommendService.saveOrUpdate(travelCommend);
        return new JSONResult();
   }

}
